package com.pdwl.cashoutpay.controller;

import com.pdwl.cashoutpay.entity.Merchant;
import com.pdwl.cashoutpay.entity.Trade;
import com.pdwl.cashoutpay.entity.res.BaseEntity;
import com.pdwl.cashoutpay.service.MerchantRegisterService;
import com.pdwl.cashoutpay.service.OrderService;
import com.pdwl.cashoutpay.util.ContanGobal;
import com.pdwl.cashoutpay.util.HttpUtil;
import com.pdwl.cashoutpay.util.JsonUtil;
import com.pdwl.cashoutpay.util.LogUtils;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;


@Controller
@RequestMapping("reset")
public class resetRate {

    @Autowired
    MerchantRegisterService merchantRegisterService;

    @RequestMapping("resetRate")
    public  void reset(HttpServletRequest request, HttpServletResponse response) throws  Exception {
        LogUtils.logInfo("开始重置费率");
        response.setHeader("Content-type", "textml;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        String res=null;
        JSONObject map = JsonUtil.getJson(request);
        BaseEntity baseEntity = new BaseEntity();

        String pMercId=map.getString("pMercId");
        String outMerchantCode=map.getString("outMerchantCode");
        Merchant merchant=merchantRegisterService.queryByOutMerchCode(outMerchantCode);

        if ("".equals(pMercId) || pMercId == null) {
            LogUtils.logInfo("没有代理商户号");
            baseEntity.setError_code("20001");
            baseEntity.setError_message("pMercId参数缺失！");
        }else{
            Merchant agent = merchantRegisterService.queryByOutMerchCode(pMercId);//查询代理商信息
            BigDecimal agentTraderate = new BigDecimal(agent.getTraderate());
            BigDecimal tradeRate=new BigDecimal(map.getString("tradeRate"));
            if (agentTraderate.compareTo(tradeRate) >= 0) {
                LogUtils.logInfo("商户费率需大于等于通道费率");
                baseEntity.setError_code("20003");
                baseEntity.setError_message("商户费率需大于等于通道费率！");
                JSONObject json=JSONObject.fromObject(baseEntity);
                 res= json.toString();
            }else {
                String url = "http://114.55.107.88:8080/thub/merchant/resetRate";
                Map payReq = new HashMap<String, String>();
                payReq.put("subMerchantCode",merchant.getSubmerchantcode());
                payReq.put("merchantCode", ContanGobal.OURMERCHANTCODE);
                payReq.put("tradeRate",map.getString("tradeRate"));
                payReq.put("fastpayRate",map.getString("tradeRate"));
                res=HttpUtil.formUpload(url, payReq);
                JSONObject json=JSONObject.fromObject(res);
                if("0".equals(json.getString("error_code"))){
                    Merchant merchant1=new Merchant();
                    merchant1.setOutmerchantcode(outMerchantCode);
                    merchant1.setTraderate(map.getString("tradeRate"));
                    merchantRegisterService.update(merchant1);
                }
                System.out.println("收到数据"+res);
            }
            response.getWriter().write(res);
        }


    }

    @RequestMapping("cardChange")
    public  void cardChange(HttpServletRequest request, HttpServletResponse response) throws  Exception {
        LogUtils.logInfo("开始结帐卡变更");
        response.setHeader("Content-type", "textml;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        JSONObject map = JsonUtil.getJson(request);
        String outMerchantCode=map.getString("outMerchantCode");
        Merchant merchant=merchantRegisterService.queryByOutMerchCode(outMerchantCode);

        String url = "http://114.55.107.88:8080/thub/merchant/cardChange";

        Map payReq = new HashMap<String, String>();
        payReq.put("subMerchantCode",merchant.getSubmerchantcode());
        payReq.put("merchantCode", ContanGobal.OURMERCHANTCODE);
        payReq.put("AccNo",map.getString("AccNo"));
        payReq.put("mobileBank",map.getString("mobileBank"));
        payReq.put("alliedBankCode",map.getString("alliedBankCode"));
        payReq.put("openBank",map.getString("openBank"));
        payReq.put("bankCode",map.getString("bankCode"));

        String res=HttpUtil.formUpload(url, payReq);
        JSONObject json=JSONObject.fromObject(res);
        if("0".equals(json.getString("error_code"))){
            Merchant merchant1=new Merchant();
            merchant1.setOutmerchantcode(outMerchantCode);
            merchant1.setAccno(map.getString("AccNo"));
            merchant1.setContactmobile(map.getString("mobileBank"));
            merchant1.setAlliedbankcode(map.getString("alliedBankCode"));
            merchant1.setOpenbank(map.getString("openBank"));
            merchant1.setBankcode(map.getString("bankCode"));
            merchantRegisterService.update(merchant1);
        }
        System.out.println("收到数据"+res);
        response.getWriter().write(res);
    }
}
